/*     */ package net.sourceforge.appgen.connector;
/*     */ 
/*     */ import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import net.sourceforge.appgen.converter.CamelCaseConverter;
import net.sourceforge.appgen.converter.StringConverter;
import net.sourceforge.appgen.model.ConnectionInformation;
import net.sourceforge.appgen.model.Entity;
import net.sourceforge.appgen.model.Field;
import net.sourceforge.appgen.util.ConventionUtils;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.datatools.connectivity.IConnectionProfile;
import org.eclipse.datatools.connectivity.IManagedConnection;
import org.eclipse.datatools.connectivity.ProfileManager;
/*     */ 
/*     */ public class ProfileConnector
/*     */ {
/*  46 */   private StringConverter baseNameConverter = new CamelCaseConverter();
/*     */ 
/*  48 */   private StringConverter fieldNameConverter = new CamelCaseConverter();
/*     */   private ConnectionInformation connectionInformation;
/*     */ 
/*     */   public StringConverter getBaseNameConverter()
/*     */   {
/*  53 */     return this.baseNameConverter;
/*     */   }
/*     */ 
/*     */   public void setBaseNameConverter(StringConverter baseNameConverter) {
/*  57 */     this.baseNameConverter = baseNameConverter;
/*     */   }
/*     */ 
/*     */   public StringConverter getFieldNameConverter() {
/*  61 */     return this.fieldNameConverter;
/*     */   }
/*     */ 
/*     */   public void setFieldNameConverter(StringConverter fieldNameConverter) {
/*  65 */     this.fieldNameConverter = fieldNameConverter;
/*     */   }
/*     */ 
/*     */   public ConnectionInformation getConnectionInformation() {
/*  69 */     return this.connectionInformation;
/*     */   }
/*     */ 
/*     */   public void setConnectionInformation(ConnectionInformation connectionInformation) {
/*  73 */     this.connectionInformation = connectionInformation;
/*     */   }
/*     */ 
/*     */   public ProfileConnector(ConnectionInformation connectionInformation) {
/*  77 */     setConnectionInformation(connectionInformation);
/*     */   }
/*     */ 
/*     */   public static String[] getProfileNames() {
/*  81 */     ProfileManager profileManager = ProfileManager.getInstance();
/*  82 */     IConnectionProfile[] profiles = profileManager.getProfiles();
/*     */ 
/*  84 */     List<String> profileNames = new ArrayList<String>();
/*     */ 
/*  86 */     if (profiles != null) {
/*  87 */       for (IConnectionProfile profile : profiles) {
/*  88 */         if (profile.getCategory().getId().equals("org.eclipse.datatools.connectivity.db.category")) {
/*  89 */           profileNames.add(profile.getName());
/*     */         }
/*     */       }
/*     */     }
/*     */ 
/*  94 */     return (String[])profileNames.toArray(new String[profileNames.size()]);
/*     */   }
/*     */ 
/*     */   public IConnectionProfile getProfile() {
/*  98 */     ProfileManager profileManager = ProfileManager.getInstance();
/*  99 */     IConnectionProfile profile = profileManager.getProfileByName(this.connectionInformation.getName());
/*     */ 
/* 101 */     return profile;
/*     */   }
/*     */ 
/*     */   public Properties getBaseProperties() {
/* 105 */     IConnectionProfile profile = getProfile();
/*     */ 
/* 107 */     return profile.getBaseProperties();
/*     */   }
/*     */ 
/*     */   public IStatus connect() {
/* 111 */     IConnectionProfile profile = getProfile();
/*     */ 
/* 113 */     if (profile.getConnectionState() == 1) {
/* 114 */       return Status.OK_STATUS;
/*     */     }
/*     */ 
/* 117 */     if (profile == null) {
/* 118 */       throw new RuntimeException("Can't find profile by name: " + this.connectionInformation.getName());
/*     */     }
/*     */ 
/* 121 */     return profile.connect();
/*     */   }
/*     */ 
/*     */   public IStatus disconnect() {
/* 125 */     IConnectionProfile profile = getProfile();
/*     */ 
/* 127 */     if (profile.getConnectionState() == 0) {
/* 128 */       return Status.OK_STATUS;
/*     */     }
/*     */ 
/* 131 */     if (profile == null) {
/* 132 */       throw new RuntimeException("Can't find profile by name: " + this.connectionInformation.getName());
/*     */     }
/*     */ 
/* 135 */     return profile.disconnect();
/*     */   }
/*     */ 
/*     */   public Connection getRawConnection(IConnectionProfile profile) {
/* 139 */     if (profile.getConnectionState() != 1) {
/* 140 */       connect();
/*     */     }
/*     */ 
/* 143 */     IManagedConnection managedConnection = null;
/*     */ 
/* 145 */     managedConnection = profile.getManagedConnection("java.sql.Connection");
/*     */ 
/* 147 */     Connection connection = (Connection)managedConnection.getConnection().getRawConnection();
/*     */ 
/* 149 */     return connection;
/*     */   }
/*     */ 
/*     */   public List<Entity> getEntityList(IProgressMonitor monitor) throws Exception {
/* 153 */     List entityList = new ArrayList();
/*     */ 
/* 155 */     IConnectionProfile profile = getProfile();
/*     */ 
/* 157 */     if (profile == null) {
/* 158 */       throw new RuntimeException("Can't find profile by name: " + this.connectionInformation.getName());
/*     */     }
/*     */ 
/* 161 */     Connection connection = null;
/*     */ 
/* 163 */     DatabaseMetaData databaseMetadata = null;
/*     */     try
/*     */     {
/* 166 */       connection = getRawConnection(profile);
/*     */ 
/* 168 */       databaseMetadata = connection.getMetaData();
/*     */ 
/* 170 */       String catalog = connection.getCatalog();
/* 171 */       String schema = this.connectionInformation.getSchema();
/*     */ 
/* 173 */       ResultSet rs = null;
/*     */ 
/* 175 */       if ((schema != null) && (schema.length() == 0)) {
/* 176 */         schema = null;
/*     */       }
/*     */ 
/* 179 */       rs = databaseMetadata.getTables(catalog, schema, "%", new String[] { "TABLE", "VIEW" });
/*     */       try
/*     */       {
/* 182 */         while (rs.next()) {
/* 183 */           String tableCat = rs.getString("TABLE_CAT");
/* 184 */           String tableSchem = rs.getString("TABLE_SCHEM");
/* 185 */           String tableName = rs.getString("TABLE_NAME");
/*     */ 
/* 187 */           Entity entity = new Entity();
/* 188 */           entity.setTableName(tableName);
/* 189 */           entity.setBaseName(getBaseNameConverter().convert(tableName));
/* 190 */           entity.setCreate(false);
/* 191 */           entity.setAllFieldSelection(true);
/*     */ 
/* 193 */           entity.setFieldList(getFieldList(databaseMetadata, tableCat, tableSchem, entity));
/*     */ 
/* 195 */           entityList.add(entity);
/*     */ 
/* 197 */           if (monitor.isCanceled())
/*     */           {
/*     */             break;
/*     */           }
/* 201 */           monitor.subTask(tableName);
/* 202 */           monitor.worked(1);
/*     */         }
/*     */       } catch (Exception e1) {
/* 205 */         throw e1;
/*     */       } finally {
/* 207 */         if (rs != null)
/*     */           try {
/* 209 */             rs.close(); } catch (Exception localException1) {
/*     */           } 
/*     */       }try { rs.close();
/*     */       } catch (Exception localException2)
/*     */       {
/*     */       }
/*     */     }
/*     */     catch (Exception e) {
/* 215 */       throw e;
/*     */     }
/*     */ 
/* 219 */     return entityList;
/*     */   }
/*     */ 
/*     */   public int getEntityListCount() throws Exception {
/* 223 */     int count = 0;
/*     */ 
/* 225 */     IConnectionProfile profile = getProfile();
/*     */ 
/* 227 */     if (profile == null) {
/* 228 */       throw new RuntimeException("Can't find profile by name: " + this.connectionInformation.getName());
/*     */     }
/*     */ 
/* 231 */     Connection connection = null;
/*     */ 
/* 233 */     DatabaseMetaData databaseMetadata = null;
/*     */     try
/*     */     {
/* 236 */       connection = getRawConnection(profile);
/*     */ 
/* 238 */       databaseMetadata = connection.getMetaData();
/*     */ 
/* 240 */       String catalog = connection.getCatalog();
/* 241 */       String schema = this.connectionInformation.getSchema();
/*     */ 
/* 243 */       ResultSet rs = null;
/*     */ 
/* 245 */       if ((schema != null) && (schema.length() == 0)) {
/* 246 */         schema = null;
/*     */       }
/*     */ 
/* 249 */       rs = databaseMetadata.getTables(catalog, schema, "%", new String[] { "TABLE", "VIEW" });
/*     */       try
/*     */       {
/* 252 */         while (rs.next())
/* 253 */           count++;
/*     */       }
/*     */       catch (Exception e1) {
/* 256 */         throw e1;
/*     */       } finally {
/* 258 */         if (rs != null)
/*     */           try {
/* 260 */             rs.close(); } catch (Exception localException1) {
/*     */           } 
/*     */       }try { rs.close();
/*     */       } catch (Exception localException2)
/*     */       {
/*     */       }
/*     */     }
/*     */     catch (Exception e) {
/* 266 */       throw e;
/*     */     }
/*     */ 
/* 270 */     return count;
/*     */   }
/*     */ 
/*     */   private List<Field> getFieldList(DatabaseMetaData databaseMetaData, String catalog, String schema, Entity entity) throws Exception {
/* 274 */     List<Field> fieldList = new ArrayList();
/*     */ 
/* 276 */     ResultSet rs = null;
/* 277 */     ResultSet pkRs = null;
/*     */     try
/*     */     {
/* 280 */       rs = databaseMetaData.getColumns(catalog, schema, entity.getTableName(), "%");
/*     */       int columnSize;
/* 282 */       while (rs.next()) {
				  String remarks=rs.getString("REMARKS");
/* 283 */         String columnName = rs.getString("COLUMN_NAME");
/* 284 */         String typeName = rs.getString("TYPE_NAME");
/* 285 */         int dataType = rs.getInt("DATA_TYPE");
/* 286 */         columnSize = rs.getInt("COLUMN_SIZE");
/* 287 */         int decimalDegit = rs.getInt("DECIMAL_DIGITS");
/* 288 */         boolean nullable = rs.getShort("NULLABLE") == 1;
/* 289 */         String fieldType = ConventionUtils.getJavaType(dataType, columnSize, decimalDegit);
/* 290 */         boolean lob = ConventionUtils.isLobColumn(dataType);
/*     */ 
/* 292 */         Field field = new Field(entity);
				  field.setDecimalDegit(decimalDegit);;
/*     */ 		  field.setRemarks(remarks);
/* 294 */         field.setColumnName(columnName);
/* 295 */         field.setFieldName(getFieldNameConverter().convert(columnName));
/* 296 */         field.setColumnType(typeName);
/* 297 */         field.setFieldType(fieldType);
/* 298 */         field.setLob(lob);
/* 299 */         field.setColumnSize(columnSize);
/* 300 */         field.setNullable(nullable);
/* 301 */         field.setCreate(true);
/*     */ 
/* 303 */         fieldList.add(field);
/*     */       }
/*     */ 
/* 306 */       pkRs = databaseMetaData.getPrimaryKeys(catalog, schema, entity.getTableName());
/*     */ 
/* 308 */       int pkPosition = 1;
/* 309 */       while (pkRs.next()) {
/* 310 */         String pkColumnName = pkRs.getString("COLUMN_NAME");
/* 311 */         for (Field field : fieldList)
/* 312 */           if (field.getColumnName().equals(pkColumnName))
/* 313 */             field.setPkPosition(pkPosition++);
/*     */       }
/*     */     }
/*     */     catch (Exception e)
/*     */     {
/* 318 */       throw e;
/*     */     } finally {
/* 320 */       if (rs != null)
/*     */         try {
/* 322 */           rs.close();
/*     */         }
/*     */         catch (Exception localException1) {
/*     */         }
/* 326 */       if (pkRs != null)
/*     */         try {
/* 328 */           pkRs.close();
/*     */         }
/*     */         catch (Exception localException2)
/*     */         {
/*     */         }
/*     */     }
/* 334 */     return fieldList;
/*     */   }
/*     */ 
/*     */   public String[] getSchemas() {
/* 338 */     IConnectionProfile profile = getProfile();
/*     */ 
/* 340 */     Connection connection = null;
/*     */ 
/* 342 */     DatabaseMetaData databaseMetadata = null;
/*     */     try
/*     */     {
/* 345 */       connection = getRawConnection(profile);
/*     */ 
/* 347 */       databaseMetadata = connection.getMetaData();
/*     */     } catch (Exception e) {
/* 349 */       throw new RuntimeException(e);
/*     */     }
/*     */ 
/* 353 */     return getSchemas(databaseMetadata);
/*     */   }
/*     */ 
/*     */   public String[] getSchemas(DatabaseMetaData databaseMetadata) {
/* 357 */     List schemas = new ArrayList();
/*     */ 
/* 359 */     ResultSet rs = null;
/*     */     try
/*     */     {
/* 362 */       rs = databaseMetadata.getSchemas();
/*     */ 
/* 364 */       while (rs.next()) {
/* 365 */         String schema = rs.getString("TABLE_SCHEM");
/* 366 */         schemas.add(schema);
/*     */       }
/*     */     } catch (Exception e) {
/* 369 */       throw new RuntimeException(e);
/*     */     } finally {
/* 371 */       if (rs != null)
/*     */         try {
/* 373 */           rs.close();
/*     */         }
/*     */         catch (SQLException localSQLException1)
/*     */         {
/*     */         }
/*     */     }
/* 379 */     return (String[])schemas.toArray(new String[schemas.size()]);
/*     */   }
/*     */ }

/* Location:           C:\Users\lzl6066\Downloads\net.sourceforge.appgen_1.2.0\plugins\net.sourceforge.appgen_1.2.0.201002031718\
 * Qualified Name:     net.sourceforge.appgen.connector.ProfileConnector
 * JD-Core Version:    0.6.0
 */